const {getAllWindow,selectScreen,startScreen,endScreen} = require('./recordscreen.js')




let allWindos = getAllWindow();

allWindos.then(data=>{
    let allWindowDom = document.getElementById('allWindows')
   // console.log('allWindos='+JSON.stringify(data))
    for (let index = 0; index < data.length; index++) {
        const element = data[index];
        // console.log('element='+JSON.stringify(element))
        var objOption = document.createElement("OPTION");  
        objOption.text = element.name;  
        objOption.value = element.id;  
        allWindowDom.options.add(objOption);  
    }
})


function screen(){
    console.log('screen.......')
    let myselect = document.getElementById('allWindows')
    var index=myselect.selectedIndex ; 
    let value = myselect.options[index].value;
    console.log('screen.......'+value)
    let p = selectScreen(value);
    p.then(data=>{
        play(data)
    }).catch(e=>{
        console.error("e="+e)
    })
}

function play(stream) {
    let video = document.getElementById('screen-video')
    video.srcObject = stream
    video.onloadedmetadata = function() {
        video.play()
    }
}

let isStart =false
function startScreenView(){
    if(isStart){
        return;
    }
    let path  ="/data/screen/a.mp4"
    startScreen(path)
    isStart = true
    
}
function endScreenView(){
    if(!isStart){
        return;
    }
    endScreen()
}

let screenDom = document.getElementById('select')
screenDom.onclick=screen;


let startScreenDom = document.getElementById('startScreen')
startScreenDom.onclick=startScreenView;

let endScreenDom = document.getElementById('endScreen')
endScreenDom.onclick=endScreenView;



